package com.example.Adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.shopapp.R;
import com.example.viewmodel.Groupon;

import java.util.List;

    public class GrouponAdapter extends RecyclerView.Adapter<GrouponAdapter.ViewHolder> {
    public Context ctx;
    public List<Groupon> groupons;

    public GrouponAdapter(Context ctx, List<Groupon> groupons) {
        this.ctx = ctx;
        this.groupons = groupons;
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(this.ctx).inflate(R.layout.home_groupon_item, parent, false);
        GrouponAdapter.ViewHolder viewHolder = new GrouponAdapter.ViewHolder(view);

        return viewHolder;
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
//        holder.home_groupon_img
        holder.home_groupon_txt1.setText(this.groupons.get(position).getName()+"");
        holder.home_groupon_txt2.setText(this.groupons.get(position).getBrief()+"");
        holder.home_groupon_grouponMember.setText(this.groupons.get(position).getGrouponMember()+"人成团");
        holder.home_groupon_grouponDiscount.setText(this.groupons.get(position).getGrouponDiscount()+"元再减");
        holder.home_groupon_grouponPrice.setText("￥"+this.groupons.get(position).getGrouponPrice());
        holder.home_groupon_retailPrice.setText("￥"+this.groupons.get(position).getRetailPrice());

        // 渲染图片（假设 groupon 对象有 getImageUrl() 方法）
        String imageUrl = this.groupons.get(position).getPicUrl();
        Glide.with(holder.itemView.getContext())
                .load(imageUrl) // 图片 URL
                .into(holder.home_groupon_img); // 目标 ImageView
    }

    @Override
    public int getItemCount() {
        return this.groupons.size();
    }

    class ViewHolder extends RecyclerView.ViewHolder{
        public ImageView home_groupon_img;
        public TextView home_groupon_txt1;
        public TextView home_groupon_txt2;
        public TextView home_groupon_grouponMember;
        public TextView home_groupon_grouponDiscount;
        public TextView home_groupon_grouponPrice;
        public TextView home_groupon_retailPrice;

        public ViewHolder(@NonNull View itemView) {
            super(itemView);
            this.home_groupon_img = itemView.findViewById(R.id.home_groupon_img);
            this.home_groupon_txt1 = itemView.findViewById(R.id.home_groupon_txt1);
            this.home_groupon_txt2 = itemView.findViewById(R.id.home_groupon_txt2);
            this.home_groupon_grouponMember = itemView.findViewById(R.id.home_groupon_grouponMember);
            this.home_groupon_grouponDiscount = itemView.findViewById(R.id.home_groupon_grouponDiscount);
            this.home_groupon_grouponPrice = itemView.findViewById(R.id.home_groupon_grouponPrice);
            this.home_groupon_retailPrice = itemView.findViewById(R.id.home_groupon_retailPrice);
        }
    }
}
